/**
 * Candidate number: 35834
 */
package tv.starfu.systems;

import com.artemis.Component;
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.EntityProcessingSystem;
import com.jme3.app.SimpleApplication;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import tv.starfu.components.InputControl;
import tv.starfu.components.SpaceshipControl;

/**
 * process all entities (player) with INPUTCONTROL and SPACESHIPCONTROL
 * @author nia radical
 */
public class ControllerSystem extends EntityProcessingSystem {

    private InputManager inputManager;
    private ComponentMapper<InputControl> inputMapper;
    private ComponentMapper<SpaceshipControl> spaceshipMapper;
    
    private InputControl inputCtrl;
    private SpaceshipControl spaceshipCtrl;
    
    private float accValue;
    private float steerValue;
    

    public ControllerSystem(SimpleApplication app) {
        super(InputControl.class);
        this.inputManager = app.getInputManager();
    }
    
    @Override
    public void initialize() {
        inputMapper = new ComponentMapper<InputControl>(InputControl.class, world);
        spaceshipMapper = new ComponentMapper<SpaceshipControl>(SpaceshipControl.class, world);
        setupKeys();
    }
    
    @Override
    protected void process(Entity e) {
        inputCtrl = inputMapper.get(e);
        spaceshipCtrl = spaceshipMapper.get(e);
        
        accValue = spaceshipCtrl.getAccelerationValue();
        steerValue = spaceshipCtrl.getSteeringValue();
    }

    private void setupKeys() {
        inputManager.addMapping("Forward",  new KeyTrigger(KeyInput.KEY_UP),    new KeyTrigger(KeyInput.KEY_I));
        inputManager.addMapping("Brake",    new KeyTrigger(KeyInput.KEY_DOWN),  new KeyTrigger(KeyInput.KEY_K));
        inputManager.addMapping("Left",     new KeyTrigger(KeyInput.KEY_J),     new KeyTrigger(KeyInput.KEY_LEFT));
        inputManager.addMapping("Right",    new KeyTrigger(KeyInput.KEY_L),     new KeyTrigger(KeyInput.KEY_RIGHT));
        
        inputManager.addMapping("Jump",     new KeyTrigger(KeyInput.KEY_A),     new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addMapping("Shoot",    new KeyTrigger(KeyInput.KEY_S));
        
        inputManager.addListener(analogListener, new String[]{ "Forward", "Brake", "Left", "Right" });
        inputManager.addListener(actionListener, new String[]{ "Jump", "Shoot" });
    }
    
    private ActionListener actionListener = new ActionListener() {
          public void onAction(String name, boolean keyPressed,float tpf) {
              
              if(name.equals("Brake") && !keyPressed)       {  System.out.println("Brake!");            inputCtrl.setDown(keyPressed);  }
              if(name.equals("Jump") && !keyPressed)        {  System.out.println("Jump!");             inputCtrl.setJump(keyPressed);  }
              if(name.equals("Shoot") && !keyPressed)       {  System.out.println("Shoot!");            inputCtrl.setShoot(keyPressed); }
          }
      };
    
    private AnalogListener analogListener = new AnalogListener() {
        public void onAnalog(String name, float value, float tpf) {
              if(name.equals("Forward"))     {  accValue += spaceshipCtrl.getAccelerationForce(); 
                                                spaceshipCtrl.setAccelerationValue(accValue);  }
//              else                           {  accValue -= spaceshipCtrl.getAccelerationForce(); 
//                                                spaceshipCtrl.setAccelerationValue(accValue);  }
//              
//              if(name.equals("Reverse"))     {  accValue -= spaceshipCtrl.getAccelerationValue(); 
//                                                spaceshipCtrl.setAccelerationValue(accValue);  }
//              else                           {  accValue -= spaceshipCtrl.getAccelerationValue(); 
//                                                spaceshipCtrl.setAccelerationValue(accValue);  }
//              
//              if(name.equals("Reverse"))     {  System.out.println("Reverse that!");     inputCtrl.setDown(keyPressed);  }
              
              if(name.equals("Left"))        {  steerValue += spaceshipCtrl.getSteeringForce(); 
                                                spaceshipCtrl.setSteeringValue(steerValue);  }
//              else                           {  steerValue -= spaceshipCtrl.getSteeringForce(); 
//                                                spaceshipCtrl.setSteeringValue(steerValue);  }              
              
              if(name.equals("Right"))       {  steerValue -= spaceshipCtrl.getSteeringForce(); 
                                                spaceshipCtrl.setSteeringValue(steerValue);  }
//              else                           {  steerValue += spaceshipCtrl.getSteeringForce(); 
//                                                spaceshipCtrl.setSteeringValue(steerValue);  }              
        }
    };
}
